<template>
  <div class="floating-menu">
    <!-- 购物车 -->
    <div class="menu-item cart-item" @click="goToCart">
      <div class="item-icon">
        <i class="fas fa-shopping-cart"></i>
        <span v-if="cartCount > 0" class="badge">{{ cartCount }}</span>
      </div>
      <span class="item-text">购物车</span>
    </div>

    <!-- 我的 -->
    <div class="menu-item" @click="goToProfile">
      <div class="item-icon">
        <i class="fas fa-user"></i>
      </div>
      <span class="item-text">我的</span>
    </div>

    <!-- 客服 -->
    <div class="menu-item" @click="openCustomerService">
      <div class="item-icon">
        <i class="fas fa-headset"></i>
        <span class="online-indicator"></span>
      </div>
      <span class="item-text">客服</span>
    </div>

    <!-- 回到顶部 -->
    <div class="menu-item back-to-top" @click="scrollToTop" v-show="showBackToTop">
      <div class="item-icon">
        <i class="fas fa-arrow-up"></i>
      </div>
      <span class="item-text">顶部</span>
    </div>
  </div>
</template>

<script setup>
// 手动导入store
import { useCartStore } from '@/stores/cart'

const cartStore = useCartStore()
const showBackToTop = ref(false)

// 计算购物车数量
const cartCount = computed(() => {
  if (!cartStore.cart?.items) return 3 // 模拟购物车数量
  return cartStore.cart.items.reduce((total, item) => total + item.quantity, 0)
})

// 跳转到购物车
const goToCart = () => {
  navigateTo('/cart')
}

// 跳转到个人中心
const goToProfile = () => {
  navigateTo('/user')
}

// 打开客服
const openCustomerService = () => {
  alert('客服功能开发中...')
}

// 回到顶部
const scrollToTop = () => {
  window.scrollTo({
    top: 0,
    behavior: 'smooth'
  })
}

// 监听滚动事件，显示/隐藏回到顶部按钮
onMounted(() => {
  const handleScroll = () => {
    showBackToTop.value = window.scrollY > 300
  }
  
  window.addEventListener('scroll', handleScroll)
  
  onUnmounted(() => {
    window.removeEventListener('scroll', handleScroll)
  })
})
</script>

<style lang="scss" scoped>
.floating-menu {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 0;

  .menu-item {
    width: 50px;
    height: 50px;
    background: #fff;
    border: 1px solid #e5e5e5;
    border-radius: 0;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    margin-bottom: 1px;

    &:first-child {
      border-radius: 4px 4px 0 0;
    }

    &:last-child {
      border-radius: 0 0 4px 4px;
      margin-bottom: 0;
    }

    &:hover {
      background: #e4393c;
      border-color: #e4393c;
      color: #fff;

      .item-text {
        color: #fff;
      }

      .item-icon {
        color: #fff;
      }
    }

    .item-icon {
      position: relative;
      width: 20px;
      height: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #666;
      font-size: 16px;

      .badge {
        position: absolute;
        top: -8px;
        right: -8px;
        background: #e4393c;
        color: #fff;
        font-size: 10px;
        padding: 2px 5px;
        border-radius: 10px;
        min-width: 16px;
        height: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
        line-height: 1;
        font-weight: bold;
        animation: bounce 0.6s ease-in-out;
      }

      .online-indicator {
        position: absolute;
        top: -2px;
        right: -2px;
        width: 6px;
        height: 6px;
        background: #52c41a;
        border-radius: 50%;
        border: 1px solid #fff;
      }
    }

    .item-text {
      font-size: 10px;
      color: #666;
      font-weight: 400;
      margin-top: 2px;
      line-height: 1;
    }

    // 购物车特殊样式
    &.cart-item {
      &:hover {
        .badge {
          background: #fff;
          color: #e4393c;
        }
      }
    }

    // 回到顶部特殊样式
    &.back-to-top {
      &:hover {
        background: #e4393c;
        border-color: #e4393c;
        color: #fff;
      }
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .floating-menu {
    right: 15px;
    bottom: 15px;

    .menu-item {
      width: 45px;
      height: 45px;

      .item-icon {
        width: 18px;
        height: 18px;
        font-size: 14px;
      }

      .item-text {
        font-size: 9px;
      }
    }
  }
}

// 动画效果
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes bounce {
  0%, 20%, 50%, 80%, 100% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-3px);
  }
  60% {
    transform: translateY(-2px);
  }
}

.menu-item {
  animation: fadeInUp 0.3s ease forwards;
  
  &:nth-child(1) { animation-delay: 0.1s; }
  &:nth-child(2) { animation-delay: 0.2s; }
  &:nth-child(3) { animation-delay: 0.3s; }
  &:nth-child(4) { animation-delay: 0.4s; }
}
</style>
